package com.fqgj.xjd.user.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.xjd.user.dao.UserKycDriverLicenseResultDao;
import com.fqgj.xjd.user.entity.UserKycDriverLicenseResultEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * User: liuming
 * Date: 2018-12-25
 * Time: 09:55:42.752
 */

@Repository
public class UserKycDriverLicenseResultDaoImpl extends AbstractBaseMapper<UserKycDriverLicenseResultEntity> implements UserKycDriverLicenseResultDao {

    @Override
    public void addUserKycDriverLicenseResult(UserKycDriverLicenseResultEntity entity) {
        if (Objects.isNull(entity) || StringUtils.isBlank(entity.getUserCode())) {
            return;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("userCode", entity.getUserCode());
        List<UserKycDriverLicenseResultEntity> driverLicense = this.selectByParams(param);
        if (CollectionUtils.isEmpty(driverLicense)) {
            this.insert(entity);
            return;
        }
        UserKycDriverLicenseResultEntity en = driverLicense.get(0);
        BeanUtils.copyProperties(en, en);
        this.updateByPrimaryKey(en);
    }

    @Override
    public UserKycDriverLicenseResultEntity selectByUserCode(String userCode) {
        Map<String, Object> param = new HashMap<>();
        param.put("userCode", userCode);
        return getSqlSession().selectOne(getStatement("selectByUserCode"), param);
    }
}